package com.mj.mafeed.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

import com.mj.mafeed.bean.ForumTopicJSON;
import com.mj.mafeed.bean.ForumTopicListJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.util.HttpUtils;

/**
 * 资讯
 * @author zhaominglei
 * @date 2016-1-29
 * 
 */
public class ForumTopicService extends BaseService {

	/**
	 * 商品列表
	 */
	public ForumTopicListJSON getForumTopic(String referer, int type, int page) {
		StringBuilder url = new StringBuilder(Constants.FORUMTOPIC_LIST_URL);
		url.append("?p=").append(page).append("&forumType=").append(type);
		String result = HttpUtils.doGet(url.toString());
		if (result != null && !result.equals("")) {
			try {
				ForumTopicListJSON forumTopicListJSON = new ObjectMapper().readValue(result, ForumTopicListJSON.class);
				return (forumTopicListJSON != null) ? forumTopicListJSON : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 保存
	 */
	public boolean save(String referer, ForumTopicJSON forumTopicJSON) {
		if (referer == null || forumTopicJSON == null) {
			return false;
		}
		Map<String, Object> params = packParams(referer, forumTopicJSON);
		if (params == null) {
			return false;
		}
		String result = HttpUtils.doPostWithFiles(Constants.FORUMTOPIC_SAVE_URL, params);
		if (result != null && !result.equals("")) {
			if (result.equals("true")) {
				return true;
			}
		}
		return false;
	}

	private Map<String, Object> packParams(String referer, ForumTopicJSON forumTopicJSON) {
		if (forumTopicJSON == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("referer", referer);
		params.put("content", forumTopicJSON.getContent());
		params.put("url", forumTopicJSON.getUrl());
		List<ForumTopicJSON.ForumTopicImage> forumTopicImages = forumTopicJSON.getForumTopicImages();
		for (int i = 0;(forumTopicImages != null && !forumTopicImages.isEmpty()) && i < forumTopicImages.size(); i++) {
			ForumTopicJSON.ForumTopicImage forumTopicImage = forumTopicImages.get(i);
			params.put("forumTopicImages["+i+"].file", forumTopicImage.getImageFile());
		}
		return params;
	}
	
	/**
	 * 赞
	 */
	public ForumTopicJSON praise(String referer, String forumTopicId, String username, String sign) {
		if (forumTopicId == null || forumTopicId.equals("") || Integer.valueOf(forumTopicId) < 1) {
			return null;
		}
		StringBuilder param = new StringBuilder();
		param.append("referer=").append(referer).append("&forumTopicId=").append(forumTopicId);
		param.append("&username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doPost(Constants.FORUMTOPIC_PRAISE_URL, param.toString());
		if (result != null && !result.equals("")) {
			try {
				ForumTopicJSON forumTopicJSON2 = new ObjectMapper().readValue(result, ForumTopicJSON.class);
				return (forumTopicJSON2 != null) ? forumTopicJSON2 : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 评论
	 */
	public ForumTopicJSON comment(String referer, String forumTopicId, String content, String username, String sign) {
		if (forumTopicId == null || forumTopicId.equals("") || Integer.valueOf(forumTopicId) < 1 
				|| content == null || content.equals("")) {
			return null;
		}
		StringBuilder param = new StringBuilder();
		param.append("referer=").append(referer).append("&forumTopicId=").append(forumTopicId);
		param.append("&content=").append(content);
		param.append("&username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doPost(Constants.FORUMTOPIC_COMMENT_URL, param.toString());
		if (result != null && !result.equals("")) {
			try {
				ForumTopicJSON forumTopicJSON2 = new ObjectMapper().readValue(result, ForumTopicJSON.class);
				return (forumTopicJSON2 != null) ? forumTopicJSON2 : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}	
}
